
from PyQt5.QtWidgets import QWidget, QToolTip, QDesktopWidget,QMainWindow,QTabWidget,QGridLayout,QSizePolicy,QVBoxLayout
from PyQt5.QtGui import QIcon

class BaseCenterWin(QWidget):
    def __init__(self, title='我的QT', sizeW=300, sizeH=300, icoUrl='assets/ico/logo.png'):
        super().__init__()
        self.sizeW = sizeW
        self.sizeH = sizeH
        self.title = title
        self.icoUrl = icoUrl

        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.resize(self.sizeW,self.sizeH)
        if len(self.icoUrl) > 0:
            self.setWindowIcon(QIcon(self.icoUrl))
            
        self.center()

    def center(self):
        # PyQt5.QtCore.QRect
        qr = self.frameGeometry() #获取当前窗口的矩形区域
        # PyQt5.QtCore.QPoint        
        cp = QDesktopWidget().availableGeometry().center() #获取PC屏幕中心点
        qr.moveCenter(cp)
        self.move(qr.topLeft())

class BaseMainWin(QMainWindow):
    def __init__(self,title='我的QT', sizeW=600, sizeH=600, icoUrl='assets/ico/logo.png'):
        super().__init__()
        self.sizeW = sizeW
        self.sizeH = sizeH
        self.title = title
        self.icoUrl = icoUrl
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)        
        self.resize(self.sizeW,self.sizeH)
        if len(self.icoUrl) > 0:
            self.setWindowIcon(QIcon(self.icoUrl))
        self.center()

    def center(self):
        # PyQt5.QtCore.QRect
        qr = self.frameGeometry() #获取当前窗口的矩形区域
        # PyQt5.QtCore.QPoint
        cp = QDesktopWidget().availableGeometry().center() #获取PC屏幕中心点
        qr.moveCenter(cp)
        self.move(qr.topLeft())
class BaseTabWin(BaseMainWin):
    def __init__(self,tabClsArr,tabLabelStr,title='我的QT',sizeW=600, sizeH=600, icoUrl='assets/ico/logo.png'):
        super().__init__(title,sizeW,sizeH,icoUrl)
        self.tabLabelArr = tabLabelStr.split(",")
        self.tabClsArr = tabClsArr        
        self.tabWidget = QTabWidget(self)
        self.tabWidget.resize(sizeW,sizeH)
        self.vBox = QVBoxLayout()
        # QMainWindow 有自己的布局 不能直接设置布局，那样是不能生效的,报错 ： QWidget::setLayout: Attempting to set QLayout "" on MyMainWin "", which already has a layout
        self.setCentralWidget(self.tabWidget) #先设置为CentralWidget        
        self.tabWidget.setLayout(self.vBox) #再设置layout布局 
        self.tabPanels = []
        n = len(self.tabClsArr)
        for i in range(n):            
            panel = self.tabClsArr[i]()
            self.addTab(panel,self.tabLabelArr[i])
            self.tabPanels.append(panel)

            #self.grid.addWidget(panel)        
    def addTab(self,widget,tabLabel):
        self.tabWidget.addTab(widget,tabLabel)
    def getPanel(self,index):
        return self.tabPanels[index]
    def getPanelByName(self,name):
        idx = list(self.tabLabelArr).index(name)
        return self.getPanel(idx)